/**
 * The Tooltip user-interface component class
 * @class syslib.ui.Tooltip
 * @author Gabriel Ricci
 */
syslib.ui.Tooltip = __class(syslib.ui.VisualTag, 
/**
 * @lends syslib.ui.Tooltip
 */
{
	DIRECTION_LEFT : 0, 
	DIRECTION_RIGHT : 1, 
	
	controller : {},
	tooltip : {}, 
	direction : this.DIRECTION_RIGHT,  
	page : null, 
	isMouseOver : false, 
	delay : 0.5, 
	
	prepare : function(){
		// set the class attributes
		this.controller		= {};
		this.tooltip				= {};
		this.direction			= this.DIRECTION_RIGHT;
		this.page				= null;
		this.isMouseOver	= false;
		this.delay	 			= 0.5;

		// load the style
		syslib.loadStyleForComponent("tooltip");
		
		// check the direction
		if(__valid(this.__attributes.direction)){
			var direction = this.__attributes.direction;
			if(direction == this.DIRECTION_LEFT || direction == this.DIRECTION_RIGHT){
				this.direction = direction;
			}
		}
		
		// check the remote page
		if(__valid(this.__attributes.page)){
			this.page = this.__attributes.page;
		}
		
		// check the delay
		if(__valid(this.__attributes.delay)){
			this.delay = this.__attributes.delay;
		}
		
		// set the controller
		this.controller = this.__attributes.controller;
		
		// return
		return true;
	},

	execute : function(){
		if(__valid(syslib.ui.Tooltip.__element)){
			// set the tooltip
			this.tooltip = syslib.ui.Tooltip.__element;
			
			// return
			return this.tooltip;
		} else {
			try{
				// create the dom object attributes
				var tooltipAttributes = syslib.object.extend(this.__attributes, {
					"class" : "tooltip-main"
				});
				
				// create the json
				var elemtJSON = ["div", tooltipAttributes];
		
				// create the element
				var tooltip = this.createDOM(elemtJSON);
				
				// store the element
				syslib.ui.Tooltip.__element = tooltip;
				this.tooltip = tooltip;
				
				// retorna
				return tooltip;
			} catch(e){
				return false;
			}
		}
	},

	finalize : function(){
		
	}, 
	
	onEnterBody : function(){
		// get the tooltip
		var tooltip = this.tooltip;
		
		// append to body
		if(tooltip.parentNode.tagName.toLowerCase() != "body"){
			tooltip.parentNode.removeChild(tooltip);
			document.body.appendChild(tooltip);	
		}
		
		// hide the element
		syslib.element.hide(tooltip);
		
		// check if the controller is valid
		if(!__valid(__id(this.controller))){
			// invalid controller, shows an error and return
			syslib.messages.error("syslib.ui.Tooltip.onEnterBody(): Invalid controller for tag tooltip");
			return false;
		} else {
			// get the controller dom object
			var controller = __id(this.controller);
			
			// set the events
			syslib.event.listen('mouseover', controller, function(evt){
				// set the mouse status
				this.isMouseOver = true;
				
				// clear the tooltip
				tooltip.innerHTML = "";
				
				// set an interval to show the tooltip
				setTimeout(this.showTooltip.applyTo(this), (this.delay * 1000));
			}.applyTo(this));
			syslib.event.listen('mousemove', controller, function(evt){
				// set the position
				this.setPosition(evt);
			}.applyTo(this));
			syslib.event.listen('mouseout', controller, function(evt){
				// set the mouse status
				this.isMouseOver = false;
				
				// hide the element
				syslib.element.hide(this.tooltip);
			}.applyTo(this));
			syslib.event.listen('click', controller, function(evt){
				// set the mouse status
				this.isMouseOver = false;
				
				// hide the element
				syslib.element.hide(this.tooltip);
			}.applyTo(this));
			
			// return
			return true;
		}
	}, 
	
	showTooltip : function(){
		// check if the mouse if over the container
		if(!this.isMouseOver){
			return false;
		}
		
		// check if this is an ajax tooltip
		if(this.page != null){
			new syslib.ajax.request(this.page, {
				destiny: this.tooltip,
				store: false
			});
		}
		
		// update the inner html
		this.tooltip.innerHTML = this.__innerHTML;
		
		// show the tooltip
		syslib.element.show(this.tooltip);
	}, 
	
	setPosition : function(evt){
		// check if the mouse is over
		if(!this.isMouseOver){
			return false;
		}
		
		// get some needed information
		var element 	= __id(this.controller);
		var mouseX	= syslib.event.mouseX(evt);
		var mouseY	= syslib.event.mouseY(evt);
		var left			= 0;
		
		// get the left position
		if(this.direction == this.DIRECTION_LEFT){
			var tooltipSize		= syslib.ui.size.getOffsets(this.tooltip);
			var controllerSize	= syslib.ui.size.getOffsets(element);
			
			left = mouseX - tooltipSize.width + controllerSize.width;
		} else {
			left = mouseX;
		}
		
		// update the element style
		syslib.element.updateStyle(this.tooltip, {
			"position" : "absolute",
			"margin" : 0,
			"top" : (mouseY + 25) +"px",
			"left" : left + "px"
		});
	}
});

// static variables
syslib.ui.Tooltip.__element = null;